package com.hsq.ndmall.auth.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.hsq.ndmall.auth.constant.MessageConstant;
import com.hsq.ndmall.auth.dto.MemberDto;
import com.hsq.ndmall.auth.dto.SecurityUser;
import com.hsq.ndmall.auth.dto.UserDTO;
import com.hsq.ndmall.auth.feign.UmsFeign;
import com.hsq.ndmall.common.utils.ListFromEntity2VoUtils;
import com.hsq.ndmall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName UserServiceImpl.java
 * @Description TODO  用于加载用户的信息
 * @createTime 2021年08月23日 16:52:00
 */
@Service
public class UserServiceImpl implements UserDetailsService {
    private List<UserDTO> userList;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    UmsFeign umsFeign;

    @PostConstruct
    public void initData() {
        String password = passwordEncoder.encode("123456");
        System.out.println(password);
        userList = new ArrayList<>();
        userList.add(new UserDTO(1L, "macro", password, 1, CollUtil.toList("ADMIN")));
        userList.add(new UserDTO(2L, "andy", password, 1, CollUtil.toList("TEST")));
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //  从数据库获取用户信息
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("wx_openid", username);
        R memberR = umsFeign.list(paramMap);
        // 解释数据
        List userInfoMapList = MapUtil.get((Map) memberR.get("page"), "list", List.class);
        // id
        Integer id = MapUtil.get((Map) userInfoMapList.get(0), "id", Integer.class);
        // 用户名
        String username1 = MapUtil.get((Map) userInfoMapList.get(0), "username", String.class);
        // 密码
        String password1 = MapUtil.get((Map) userInfoMapList.get(0), "password", String.class);
        // 状态
        Integer status = MapUtil.get((Map) userInfoMapList.get(0), "status", Integer.class);

        List<UserDTO> userDtoList = new ArrayList<>();
                                userDtoList.add(new UserDTO(id.longValue(),username1,password1,status,CollUtil.toList("ADMIN")));

        // List<UserDTO> findUserList = userList.stream().filter(item -> item.getUsername().equals(username)).collect(Collectors.toList());
        if (CollUtil.isEmpty(userDtoList)) {
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        SecurityUser securityUser = new SecurityUser(userDtoList.get(0));
        if (!securityUser.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!securityUser.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!securityUser.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!securityUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        return securityUser;
    }
}
